'use strict';
let db = uniCloud.database({
    throwOnNotFound: false
})
const {
    verifyToken
} = require("login-common")
exports.main = async (event, context) => {
    //event为客户端上传的参数
    const {
        api,
    } = event
    const dbCmd = db.command // 取指令
    const payload = verifyToken(event.token) // token携带的信息解密出来 得到 openid
    // 下方为publishBook.vue页面的api
    if (api === 'publishBook') {
        return await db.collection("bookList").add({
            openid: payload.openid,
            content: event.content,
            bookname: event.bookname,
            word: event.word,
            type: event.type,
            fromtype: "扫书宝典",
            createTime: Date.now(),
            author: event.nickName
        })
    }
    // myScanbook.vue的API
    if (api === 'getMyScanBook') {
        return await db.collection("bookList").where({
            openid: payload.openid
        }).orderBy('createTime', 'desc').get()
    }
    if (api === 'delScanBookDetail') {
        return await db.collection("bookList").doc(
            event._id
        ).remove()
    }
    // 下方为bookRankList.vue页面的api
    if (api === 'getBookRankList') {
        return await db.collection("bookList").orderBy('word', 'desc').limit(100).get()
    }
    if (api === 'getBookRankListDetail') return await db.collection("bookList").where({
        _id: event._id
    }).get()
    // 下方为 首页.vue页面的api
    if (api === 'getTimePublish') {
        return await db.collection("bookList").orderBy('createTime', 'desc').limit(10).get()
    }
    if (api === 'getBookWeb') {
        return await db.collection("bookWeb").get()
    }
    if (api === 'getTerminology') {
        return await db.collection("terminology").get()
    }
    if (api === 'getStatement') {
        return await db.collection("statement").get()
    }
    if (api === 'getBasicInfo') {
        return await db.collection("basicInfo").get()
    }
    // 下方为 seach.vue页面的api
    if (api === 'getSeachBook') { // 模糊查询
        // const dbCmd = db.command // 取指令
        if (event.sort === 'desc') return await db.collection("bookList").where(dbCmd.or({ // 查询两个字段
            bookname: new RegExp(event.keyword, 'i')
        }, {
            content: new RegExp(event.keyword, 'i')
        }, {
            author: new RegExp(event.keyword, 'i')
        })).orderBy('word', 'desc').get()
        if (event.sort === 'asc') return await db.collection("bookList").where(dbCmd.or({ // 查询两个字段
            bookname: new RegExp(event.keyword, 'i')
        }, {
            content: new RegExp(event.keyword, 'i')
        }, {
            author: new RegExp(event.keyword, 'i')
        })).orderBy('word', 'asc').get()
        if (event.sort === 'author') {
            return await db.collection("bookList").where({
                author: new RegExp(event.keyword)
            }).get()
        }
        if (event.sort === 'accurate') { // 精确书名
            return await db.collection("bookList").where({
                bookname: event.keyword
            }).get()
        }
        if (event.sort === 'vague') { // 模糊书名
            return await db.collection("bookList").where({
                bookname: new RegExp(event.keyword)
            }).get()
        }
        // 查询一个字段 db.collection.where({name:new RegExp(x)}).get()
        return await db.collection("bookList").where(dbCmd.or({ // 查询两个字段
            bookname: new RegExp(event.keyword, 'i')
        }, {
            content: new RegExp(event.keyword, 'i')
        }, {
            author: new RegExp(event.keyword, 'i')
        })).orderBy('word', 'asc').limit(event.pgSize).get({
            getCount: true
        })
    }
    //返回数据给客户端
    return event
};
